﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Da = VLUNewsData;
using DaMo = VLUNewsData.EntityDataModel;
using BuUt = VLUNewsBusiness.Utilities;
using System.Data.Objects;

namespace VLUNewsBusiness.VLUNews
{
    public class HtmlContentTracker
    {
        #region Base members
        private static HtmlContentTracker _Instance = null;
        public static HtmlContentTracker Instance()
        {
            if (_Instance == null) _Instance = new HtmlContentTracker();
            return _Instance;
        }

        public static void Release()
        {
            if (_Instance != null)
                GC.SuppressFinalize(_Instance);
        }
        #endregion Base members

        #region Save
        public int SaveSentToRole(int pHtmlContentID, int pSenderID, int pRolerID)
        {
            int result = -1;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                DaMo.VluNewsHtmlContentTracker model = new DaMo.VluNewsHtmlContentTracker();
                ent.VluNewsHtmlContentTrackers.AddObject(model);
                // Set informations.
                model.Status = (int)BuUt.StatusTrackerEnums.Up;
                model.HtmlContentID = pHtmlContentID;
                model.SenderID = pSenderID;
                model.RolerID = pRolerID;
                // Set base fields.
                model.LastModifiedByUserID = model.CreatedByUserID = pSenderID;
                model.LastModifiedOnDate = model.CreatedOnDate = DateTime.Now;
                // Save.
                result = ent.SaveChanges();
            }
            return result;
        }
        public int SaveSentToUser(int pHtmlContentID, int pSenderID, int pRecieverID)
        {
            int result = -1;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                DaMo.VluNewsHtmlContentTracker model = new DaMo.VluNewsHtmlContentTracker();
                ent.VluNewsHtmlContentTrackers.AddObject(model);
                // Set informations.
                model.Status = (int)BuUt.StatusTrackerEnums.Up;
                model.HtmlContentID = pHtmlContentID;
                model.SenderID = pSenderID;
                model.ReceiverID = pRecieverID;
                // Set base fields.
                model.LastModifiedByUserID = model.CreatedByUserID = pSenderID;
                model.LastModifiedOnDate = model.CreatedOnDate = DateTime.Now;
                // Save.
                result = ent.SaveChanges();
            }
            return result;
        }
        public int SaveReturned(int pHtmlContentID, int pSenderID, int pRecieverID)
        {
            int result = -1;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                DaMo.VluNewsHtmlContentTracker model = new DaMo.VluNewsHtmlContentTracker();
                ent.VluNewsHtmlContentTrackers.AddObject(model);
                // Set informations.
                model.Status = (int)BuUt.StatusTrackerEnums.Down;
                model.HtmlContentID = pHtmlContentID;
                model.SenderID = pSenderID;
                model.ReceiverID = pRecieverID;
                // Set base fields.
                model.LastModifiedByUserID = model.CreatedByUserID = pSenderID;
                model.LastModifiedOnDate = model.CreatedOnDate = DateTime.Now;
                // Save.
                result = ent.SaveChanges();
            }
            return result;
        }
        #endregion Save

        #region Get
        public int GetRecieverID(int pHtmlContentID, int pSenderID)
        {
            int result = -1;
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                // Get NewsRoleGroupID.
                ObjectResult<string> oObjectResult = ent.ExecuteStoreQuery<string>(String.Format(BuUt.SqlString.getNewsRoleGroup, BuUt.BaseSettings.NewsRoleGroupName));
                int vRoleGroupID = int.Parse(oObjectResult.First());
                // Get receiver in Tracker.
                List<int> vUserRoles = ent.UserRoles.Where(p => p.UserID == pSenderID && p.Role.RoleGroupID == vRoleGroupID).Select(p => p.RoleID).ToList();
                int? vSenderID = ent.VluNewsHtmlContentTrackers.Where(p => p.HtmlContentID == pHtmlContentID && p.Status == (int)BuUt.StatusTrackerEnums.Up
                    && (p.ReceiverID == pSenderID || vUserRoles.Contains(p.RolerID.Value)))
                    .OrderByDescending(p => p.HtmlContentTrackerID).Select(p => p.SenderID).FirstOrDefault();
                result = vSenderID.HasValue ? vSenderID.Value : 0;// 0: No receiver.
            }
            return result;
        }
        public List<int> GetsHtmlContentIDBySender(int pSenderID, int pRoleID)
        {
            List<int> result = new List<int>();
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                result = ent.VluNewsHtmlContentTrackers.Where(p => p.SenderID == pSenderID).Select(p => p.HtmlContentID.Value).ToList();
            }
            return result;
        }
        public List<int> GetsHtmlContentIDLastReceiver(int pSenderID, int pRoleID)
        {
            List<int> result = new List<int>();
            using (DaMo.VLUNewsEntities ent = new DaMo.VLUNewsEntities())
            {
                // Get receiver in Tracker.
                var trks = from p in ent.VluNewsHtmlContentTrackers
                         orderby p.HtmlContentTrackerID
                         group p by p.HtmlContentID into g
                         select new { HtmlContentID = g.Key };
                result = trks.Select(p => p.HtmlContentID.Value).ToList();
                //// Get receiver in Tracker.
                //var groupTrackers = from tracker in ent.VluNewsHtmlContentTrackers
                //                    orderby tracker.HtmlContentTrackerID
                //                    group new { tracker.HtmlContentID }
                //                    by tracker.HtmlContentID into grptrks
                //                    select grptrks;
                //foreach (var grp in groupTrackers)
                //{
                //    foreach (var trk in grp)
                //    {
                //        result.Add(trk.HtmlContentID.Value);
                //    }
                //}
            }
            return result;
        }
        #endregion Get
    }
}
